package giftmail;

import javax.jms.Destination;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import giftmail.model.Email;
import giftmail.service.IProducerService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", 
"classpath:spring-mybatis.xml"})
public class Junit4Gift {
	private static final Logger logger = LoggerFactory.getLogger(Junit4Gift.class);
	
	@Autowired  
    private IProducerService producerService;  
    @Autowired  
    @Qualifier("queueDestination")  
    private Destination destination; 
    @Autowired  
    @Qualifier("sessionAwareQueue")  
    private Destination sessionAwareQueue;
    @Autowired  
    @Qualifier("adapterQueue")  
    private Destination adapterQueue; 
    
    
	/**
	 * 测试用户Shiro
	 */
	@Test
	public void test1() {
		// 1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//		String name = PropertiesUtil.getProperty("shiro.name");
//		String password = PropertiesUtil.getProperty("shiro.password");
		// 2、得到SecurityManager实例 并绑定给SecurityUtils
		SecurityManager securityManager = factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);

		// 3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("demo", "123");
		try {
			// 4、登录，即身份验证
			subject.login(token);
		} catch (AuthenticationException e) {
			// 5、身份验证失败
		}

		Assert.assertEquals(true, subject.isAuthenticated()); // 断言用户已经登录

		// 6、退出
		subject.logout();
	}
	
	/**
	 * 测试JDBC的Shiro
	 */
	@Test
	public void test2() {
		//1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");

        //2、得到SecurityManager实例 并绑定给SecurityUtils
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("demo", "123");

        try {
            //4、登录，即身份验证
            subject.login(token);
        } catch (AuthenticationException e) {
            //5、身份验证失败
            e.printStackTrace();
        }

        Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录

        //6、退出
        subject.logout();
	}
	
	/**
	 * 测试MessageListener
	 */
	@Test  
    public void testSend() {  
        for (int i=0; i<2; i++) {  
            producerService.sendMessage(destination, "你好，生产者！这是消息：" + (i+1));  
        }  
    }  
	
	/**
	 * 测试SessionAwareMessageListener
	 */
	@Test  
    public void testSessionAwareMessageListener() {  
        producerService.sendMessage(sessionAwareQueue, "测试SessionAwareMessageListener");  
    } 
	
	/**
	 * 测试MessageListenerAdapter
	 */
	@Test  
    public void testMessageListenerAdapter() {  
        producerService.sendMessage(adapterQueue, "测试MessageListenerAdapter");  
    }
	
}
